package steed.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import steed.domain.GlobalParam;
import steed.exception.PathIsTopException;
import steed.util.base.BaseUtil;
import steed.util.base.PathUtil;
import steed.util.base.StringUtil;
import steed.util.file.FileUtil;
public class GetCurrentUrl extends TagSupport{
	private static final long serialVersionUID = -5584256537704686928L;
	private String lastUrl;

	public void setLastUrl(String lastUrl) {
		this.lastUrl = lastUrl;
	}

	@Override
	public int doStartTag() throws JspException {
		try {
			HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
			JspWriter out = pageContext.getOut();
			out.print(getPath(req,lastUrl));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
	public String getPath(HttpServletRequest req,String lastUrl){
		String path = req.getRequestURI().replace(GlobalParam.FOLDER.JSPPATH, "/");
		if (StringUtil.isStringEmpty(lastUrl)) {
			return FileUtil.replaceFileSuffix(path, ".act");
		}else {
			try {
				return PathUtil.getParaentPath(path) + "/" +lastUrl;
			} catch (PathIsTopException e) {
				BaseUtil.getLogger().error("获取jsp路径出错",e);
				return "";
			}
		}
	}

}
